package com.netease.ticket;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

/**
 * 将车站名称和车站代码对应起来，在启动时只需执行一次init
 * @author bjyangfan
 *
 */
public class StationInfo {
	private static String getStationUrl = "http://dynamic.12306.cn/otsweb/js/common/station_name.js";
	private static String getStationFile = "station_name.js";
	
	private static Map<String, String> stationName2CodeMap= new HashMap<String, String>();
	private static Map<String, String> stationCode2NameMap= new HashMap<String, String>();
	
	public static void main(String[] args)
	{
		init();
	}
	
	/**
	 * 初始化车站信息，只需初始化一次
	 * @return
	 */
	public static boolean init()
	{
		int num = 0;
		try{
			//从网站读取数据，更新本地文件
			downloadFile(getStationUrl);
			
			//从本地文件读取数据
			num = getStationInfoFromFile(getStationFile);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return false;
		}
		return num < 1000 ?  false : true;
	}
	
	/**
	 * 从网站下载车站的信息到文件
	 * @param url
	 * @return
	 */
	private static boolean downloadFile(String url)
	{
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(url);
		int code = 0;
		try{
			code = client.executeMethod(method);
			
	        //打印服务器返回的状态
		    System.out.println(method.getStatusLine());
		    
		    if(code != 200)
		    {
		    	return false;
		    }
		    File storeFile = new File(getStationFile);
		    FileOutputStream output = new FileOutputStream(storeFile);
		    output.write(method.getResponseBody());
		    output.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 直接从网站上解析车站信息(暂时不用该函数)
	 * @param url
	 * @return
	 * @throws Exception
	 */
	private static int getStationInfoFromUrl(String url) throws Exception
	{
		int code = 0;
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(url);
		code = client.executeMethod(method);
        
        //打印服务器返回的状态
	    System.out.println("获取在线的车站信息返回值：" + method.getStatusLine());
	    String content = new String(method.getResponseBody(), "GB2312");

	    return paseContent(content);
	}
	
	/**
	 * 从本地文件中读取车站的信息，加载到内存
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	private static int getStationInfoFromFile(String fileName) throws Exception
	{
		File file = new File(fileName);
        BufferedReader reader = null;
        
        reader = new BufferedReader(new FileReader(file));
        String tempString = null;
        String content = "";
        
        // 一次读入一行，直到读入null为文件结束
        while ((tempString = reader.readLine()) != null) {
        	content += tempString;
        }
    
        reader.close();
               
        return paseContent(content);
	}
	
	/**
	 * 解析包含车站信息的文件
	 * @param content
	 * @return
	 * @throws Exception
	 */
	private static int paseContent(String content)throws Exception
	{
		int ret = 0;
		String[] stations = content.split("@");
        for(int i = 0; i< stations.length;i++)
        {
        	String[] stationName = stations[i].split("\\|");
        	if(stationName.length < 6) continue;
        	stationName2CodeMap.put(stationName[1], stationName[2]);
        	stationCode2NameMap.put(stationName[2], stationName[1]);
        	ret++;
        }
        return ret;
	}
	
	/**
	 * 根据车站名称查找车站代码，如:北京->BJP
	 * @param name
	 * @return
	 */
	public static String getStationTeleCodeByName(String name)
	{
		return stationName2CodeMap.get(name);
	}
	
	/**
	 * 根据车站代码查找车站名称，如:BJP->北京
	 * @param code
	 * @return
	 */
	public static String getStationNameByTeleCode(String code)
	{
		return stationCode2NameMap.get(code);
	}
}
